#include "stdafx.h"
#include "ChessPawn.h"

HRESULT ChessPawn::DrawCanMove(const HWND& hwnd)
{
    HDC hdc = GetDC(hwnd);
    HPEN move_pen = CreatePen(PS_SOLID, 1, RGB(0xFF, 0x0, 0x0));
    SelectObject(hdc, move_pen);
    size_t size = m_can_move.size();
    for (int index = 0; index < size; ++index)
    {
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(DrawRectangle(hdc, m_can_move[index]))
    }

    return S_OK;
}

HRESULT ChessPawn::MoveCheck(const CHESS_POS& m_select_pos)
{
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_NULL(m_chess_board.get())

    m_can_move.clear();
    if (m_type_name == 1)
    {
        if (m_select_pos.row_index == GC_CHESS_BOARD_ROW_COUNT - 1)
        {
            return S_FALSE;
        }
        else if (m_select_pos.row_index == GC_CHESS_BOARD_ROW_COUNT - 2)
        {
            PLAYER player = e_no_player;
            CHESS_POS temp_pos;
            temp_pos.row_index = m_select_pos.row_index + 1;
            temp_pos.col_index = m_select_pos.col_index;
            RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                m_chess_board->GetChessFillByChessPos(temp_pos, player))
            if (player == e_no_player)
            {
                m_can_move.push_back(temp_pos);
            }
        }
        else
        {
            for (int index = 1; index <= PAWN_MOST_MOVE_STEP; ++index)
            {
                PLAYER player = e_no_player;
                CHESS_POS forward_pos;
                forward_pos.row_index = m_select_pos.row_index + index;
                forward_pos.col_index = m_select_pos.col_index;
                RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                    m_chess_board->GetChessFillByChessPos(forward_pos, player))
                if (player == e_no_player)
                {
                    m_can_move.push_back(forward_pos);

                    CHESS_POS left_pos;
                    left_pos.row_index = forward_pos.row_index;
                    left_pos.col_index = forward_pos.col_index - 1;
                    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                        m_chess_board->GetChessFillByChessPos(left_pos, player))
                    if (player == e_player_2)
                    {
                        m_can_move.push_back(left_pos);
                    }

                    CHESS_POS right_pos;
                    right_pos.row_index = forward_pos.row_index;
                    right_pos.col_index = forward_pos.col_index + 1;
                    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                        m_chess_board->GetChessFillByChessPos(right_pos, player))
                    if (player == e_player_2)
                    {
                        m_can_move.push_back(right_pos);
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
    else
    {
        if (m_select_pos.row_index == 0)
        {
            return S_FALSE;
        }
        else if (m_select_pos.row_index == 1)
        {
            PLAYER player = e_no_player;
            CHESS_POS temp_pos;
            temp_pos.row_index = m_select_pos.row_index - 1;
            temp_pos.col_index = m_select_pos.col_index;
            RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                m_chess_board->GetChessFillByChessPos(temp_pos, player))
            if (player == e_no_player)
            {
                m_can_move.push_back(temp_pos);
            }
        }
        else
        {
            for (int index = 1; index <= PAWN_MOST_MOVE_STEP; ++index)
            {
                PLAYER player = e_no_player;
                CHESS_POS forward_pos;
                forward_pos.row_index = m_select_pos.row_index - index;
                forward_pos.col_index = m_select_pos.col_index;
                RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                    m_chess_board->GetChessFillByChessPos(forward_pos, player))
                if (player == e_no_player)
                {
                    m_can_move.push_back(forward_pos);

                    CHESS_POS left_pos;
                    left_pos.row_index = forward_pos.row_index;
                    left_pos.col_index = forward_pos.col_index - 1;
                    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                        m_chess_board->GetChessFillByChessPos(left_pos, player))
                    if (player == e_player_1)
                    {
                        m_can_move.push_back(left_pos);
                    }

                    CHESS_POS right_pos;
                    right_pos.row_index = forward_pos.row_index;
                    right_pos.col_index = forward_pos.col_index + 1;
                    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(
                        m_chess_board->GetChessFillByChessPos(right_pos, player))
                    if (player == e_player_1)
                    {
                        m_can_move.push_back(right_pos);
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }

    return S_OK;
}